package com.free.chat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.solon.service.impl.ServiceImpl;
import com.free.chat.dao.TopicCollectRelationDao;
import com.free.chat.dao.TopicDao;
import com.free.chat.entity.ApiResult;
import com.free.chat.entity.dto.TopicCollectDTO;
import com.free.chat.entity.dto.TopicDTO;
import com.free.chat.entity.model.Topic;
import com.free.chat.entity.model.TopicCollectRelation;
import com.free.chat.entity.vo.TopicVO;
import com.free.chat.exception.ApiException;
import com.free.chat.service.TopicService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.Top;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.aspect.annotation.Service;
import org.noear.solon.data.annotation.Tran;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TopicServiceImpl extends ServiceImpl<TopicDao, Topic> implements TopicService {

    @Db
    private TopicDao topicDao;
    @Db
    private TopicCollectRelationDao topicCollectRelationDao;

    /**
     * @Author: 朝花不迟暮
     * @Description: 根据用户查询对应的帖子列表
     * @Date: 2022/11/26
     */
    @Override
    public ApiResult getTopicListByUser(TopicDTO topicDTO) {
        PageHelper.startPage(topicDTO.getPageNum(), topicDTO.getPageSize());
        Topic topic = BeanUtil.copyProperties(topicDTO, Topic.class);
        LambdaQueryWrapper<Topic> queryWrapper = new LambdaQueryWrapper<>(topic);
        List<Topic> topics = topicDao.selectList(queryWrapper);
        PageInfo<Topic> topicList = new PageInfo<>(topics);
        List<TopicVO> topicVOS = new ArrayList<>();
        topics.forEach(e -> {
            TopicVO topicVO = new TopicVO();
            BeanUtil.copyProperties(e, topicVO);
            LambdaQueryWrapper<TopicCollectRelation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TopicCollectRelation::getTopicId, e.getId())
                    .eq(TopicCollectRelation::getUserId, e.getUserId());
            TopicCollectRelation relation = topicCollectRelationDao.selectOne(lambdaQueryWrapper);
            topicVO.setCollect(relation != null);
            topicVOS.add(topicVO);
        });
        PageInfo<TopicVO> topicVoList = new PageInfo<>(topicVOS);
        topicVoList.setTotal(topicList.getTotal());
        return ApiResult.success(topicVoList);
    }

    /**
     * @Author: 朝花不迟暮
     * @Description: 收藏帖子, 如果传入的false就取消，如果是true就插入记录
     * @Date: 2022/11/28
     */
    @Override
    @Tran
    public ApiResult collectTopic(TopicCollectDTO topicCollectDTO) {
        try {
            Topic topic = topicDao.selectById(topicCollectDTO.getTopicId());
            int collectCount = topic.getCollectCount() == null ? 0 : topic.getCollectCount();
            if (topicCollectDTO.getCollect()) {
                TopicCollectRelation topicCollectRelation = BeanUtil.copyProperties(topicCollectDTO, TopicCollectRelation.class);
                topicCollectRelation.setId(IdUtil.getSnowflakeNextId());
                topicCollectRelation.setCreateTime(new Date());
                // 建立绑定关系
                topicCollectRelationDao.insert(topicCollectRelation);

                // 给topic的收藏数+1
                topic.setCollectCount(collectCount + 1);
            } else {
                LambdaQueryWrapper<TopicCollectRelation> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TopicCollectRelation::getTopicId, topicCollectDTO.getTopicId())
                        .eq(TopicCollectRelation::getUserId, topicCollectDTO.getUserId());
                topicCollectRelationDao.delete(queryWrapper);
                // 给topic的收藏数-1
                topic.setCollectCount(collectCount - 1);
            }
            topicDao.update(topic, null);
        } catch (Exception e) {
            log.error("【帖子收藏失败】", e);
            throw new ApiException("帖子收藏失败");
        }
        return ApiResult.success();
    }
}
